$(function () {
    /*评分星级组件*/
    $(".m-grade_star_num").each(function () {
        var score = $(this).parents('.m-grade-stars').find('.m-grade_star_score').html();
        var bg_wh=$(this).parent('.m-grade_star_bg').width();
        var newwidth = (score / 5) * bg_wh;
        console.log(newwidth);
        $(this).css('width', newwidth);
    });
    /*评分-----------------------------------------------------------------------------------------*/
    function starsNum() {
        var stars_num = $(".js-grade-bar").find("li.active").index() + 1;
        var texts = ["差评~（当前选择：5分）", "一般~（当前选择：6分）", "凑合~（当前选择：7分）", "满意~（当前选择：8分）", "非常满意~（当前选择：9分）"];
        var $grade_text = $(".js-grade-text");
        $(".js-grade-text").text(texts[stars_num - 1]);
        $(".js-select-grade").children("option").eq(stars_num - 1).attr("selected","selected")
            .siblings("option").removeAttr("selected");
        // console.log(stars_num);
    }

    $(".js-grade-bar").on('click', '.m-grade-btn', function () {
        var $this_li = $(this).parent();
        var $this_img = $(this).children("img");
        if (!$this_li.hasClass("active")) {
            $this_li.addClass("active")
                .siblings("li").removeClass("active");
        } else {
            $this_li.siblings("li").removeClass("active");
        }
        starsNum();
    });
    /*评分写评论*/
    function textareaNum($this_textarea){
        var text_length=$this_textarea.val().length;
        var max_length=$this_textarea.attr("maxlength");
        var count=max_length-text_length;
        var $counter=$this_textarea.closest(".weui-cell").find(".js-textarea-counter");
        var $num=$counter.children("span");
        $num.text(text_length);
        if(count<=0){
            $counter.css({"color":"red","font-weight":"600"});
        }else{
            $counter.removeAttr("style");
        }
    }
    $(".js-textarea").keydown(function () {
        var $this=$(this);
        textareaNum($this);
    });
});